package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.UserConstants;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.LeaderAccount;
import com.tbit.uqbike.object.pojo.vo.BatchOperationMsgVO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.LeaderAccountService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.DateUtils;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.RequestContext;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 领导账号Controller
 *
 * @author jing
 * @date 2021-05-25
 */
@RestController
@RequestMapping("/leader")
public class LeaderAccountController extends BaseController {
    @Autowired
    private LeaderAccountService leaderAccountService;

    /**
     * 查询领导账号列表
     */
    @GetMapping("/list")
    public JsonResult list(LeaderAccount leaderAccount) {
        leaderAccountService.checkExpireUser();
        startPage();
        List<LeaderAccount> list = leaderAccountService.selectLeaderAccountList(leaderAccount);
        return JsonResult.ok(list, (int) new PageInfo<>(list).getTotal());
    }


    /**
     * 新增领导账号
     */
    @PostMapping
    public JsonResult add(@RequestBody LeaderAccount leaderAccount) {
        RequestContext context = getLogin(ServletUtils.getRequest()).context;
        if (UserConstants.NOT_UNIQUE.equals(leaderAccountService.checkPhoneUnique(leaderAccount))) {
            throw new BaseException(SystemCodeEnum.ACCOUNT_PHONE_HAD_EXIT);
        }
        return JsonResult.toAjax(leaderAccountService.insertLeaderAccount(leaderAccount), context);
    }

    @PostMapping("/batchAdd")
    public Result<BatchOperationMsgVO<String>> batchAdd(@RequestBody LeaderAccount leaderAccount) {
        Assert.notNull(leaderAccount.getAccountId(), "accountId不能为空");
        List<String> phoneList = Arrays.asList(leaderAccount.getPhone().split(","));

        List<String> existPhone = phoneList.stream().filter(phone -> {
            LeaderAccount account = new LeaderAccount(leaderAccount.getAccountId(), phone);
            return UserConstants.NOT_UNIQUE.equals(leaderAccountService.checkPhoneUnique(account));
        }).collect(Collectors.toList());

        BatchOperationMsgVO<String> msgVO = new BatchOperationMsgVO<>();
        msgVO.setFailList(existPhone);

        phoneList.stream().filter(x -> !existPhone.contains(x))
                .forEach(phone -> {
                    leaderAccount.setPhone(phone);
                    msgVO.putSuccessValue(phone);
                    leaderAccountService.insertLeaderAccount(leaderAccount);
                });
        return Result.success(msgVO);
    }

    /**
     * 修改领导账号
     */
    @PutMapping
    @OperateLog(module = ModuleEnum.MAINACCOUNTUSER, methods = MethodEnum.LEADER_ACCOUNT_UPDATE)
    public JsonResult edit(@RequestBody LeaderAccount leaderAccount) {
        RequestContext context = getLogin(ServletUtils.getRequest()).context;
        if (leaderAccount.getExpireTime().getTime() >= DateUtils.getNowDate().getTime()) {
            leaderAccount.setEnable(1);
        }
        return JsonResult.toAjax(leaderAccountService.updateLeaderAccount(leaderAccount), context);
    }

    @PutMapping("/batchUpdate")
    public JsonResult batchUpdate(@RequestBody LeaderAccount leaderAccount) {
        BatchOperationMsgVO<String> msgVO = new BatchOperationMsgVO<>();
        Assert.notNull(leaderAccount.getAccountId(), "accountId不能为空");
        List<String> phoneList = Arrays.asList(leaderAccount.getPhone().split(","));
        if (leaderAccount.getExpireTime().getTime() >= DateUtils.getNowDate().getTime()) {
            leaderAccount.setEnable(1);
        }
        phoneList.forEach(x -> {
            leaderAccount.setPhone(x);
            int rowCount = leaderAccountService.updateLeaderAccount(leaderAccount);
            if (rowCount > 0) {
                msgVO.putSuccessValue(x);
            } else {
                msgVO.putFailValue(x);
            }
        });
        return JsonResult.ok(msgVO);
    }

    /**
     * 删除领导账号
     */
    @DeleteMapping
    @OperateLog(module = ModuleEnum.MAINACCOUNTUSER, methods = MethodEnum.LEADER_ACCOUNT_DEL)
    public JsonResult remove(LeaderAccount leaderAccount) {
        RequestContext context = getLogin(ServletUtils.getRequest()).context;
        return JsonResult.toAjax(leaderAccountService.deleteLeaderAccountByIds(leaderAccount), context);
    }
}
